package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.bac.BacAntClassDto;
import com.ysd.lis.entity.bac.BacAntClass;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.mapper.bac.BacAntClassMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.bac.BacAntClassParam;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.bac.BacAntClassService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_Model 服务实现类
 * </p>
 *
 * @author prs
 * @since 2025-03-07
 */
@Service
public class BacAntClassServiceImpl extends ServiceImpl<BacAntClassMapper, BacAntClass> implements BacAntClassService {
    @Autowired
    BacAntClassMapper bacAntClassMapper;

    @Override
    public Result findBacAntClassList(BacAntClassParam pageParam) {
        MPJLambdaWrapper<BacAntClass> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(BacAntClass::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAntClass::getAntClassNo, pageParam.getSeachValue())
                    .or().like(BacAntClass::getAntClassNa,pageParam.getSeachValue())
                    .or().like(BacAntClass::getInputCode,pageParam.getSeachValue())
                    .or().like(BacAntClass::getWhonetCode,pageParam.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(BacAntClass::getSeq);
        List<BacAntClass> supplierPage = bacAntClassMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addBacAntClass(BacAntClass bacAntClass) {
        int insert = bacAntClassMapper.insert(bacAntClass);
        if(insert>0){
            return Result.succ(1,"添加成功",bacAntClass);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editBacAntClass(BacAntClass supplier) {
        int updateById = bacAntClassMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result batchDeleteBacAntClassList(BacAntClassDto dto) {
        List<BacAntClass> supplierList = dto.getBacAntClassList();
        List<String> ids = supplierList.stream().map(BacAntClass::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Override
    public Result syncBacAntClass(BacAntClassParam param) {
        CommonPageDto pageParam = new CommonPageDto();
        pageParam.setCode("antClass");
        Result dictResult = sysBasDictDetailService.findDictDetailList(pageParam);
        if(ToolsUtils.isNotEmpty(dictResult)){
            List<Map<String, Object>> data = (List<Map<String, Object>>) dictResult.getData();
            for(Map<String, Object> m : data){
                BacAntClass bacAntClass = new BacAntClass();
                bacAntClass.setAntClassNo(String.valueOf(m.get("code")));
                bacAntClass.setAntClassNa(String.valueOf(m.get("name")));
                bacAntClass.setWhonetCode(String.valueOf(m.get("standardName")));
                bacAntClass.setInputCode(String.valueOf(m.get("inputCode")));
                bacAntClass.setSeq(Integer.valueOf(String.valueOf(m.get("sx"))) );
                LambdaQueryWrapper<BacAntClass> lambdaQueryWrappers = new LambdaQueryWrapper<>();
                lambdaQueryWrappers.eq(BacAntClass::getAntClassNo, String.valueOf(m.get("code")))
                        .eq(BacAntClass::getOrgId, String.valueOf(m.get("orgId")))
                        .eq(BacAntClass::getDelFlag, 0);
                boolean b = this.saveOrUpdate(bacAntClass, lambdaQueryWrappers);
            }

        }
        return Result.succ(1, "同步结果成功", null);
    }
}
